<?php
/**
 * Description of owlulang
 *
 * @author trung
 */
class Owl_Ulang extends Owl_Db {
//put your code here

    function Owl_Ulang() {
        $this->Owl_Db('ulang');
    }

    function get_all_ulangs() {
        return $this->get_results('//ulang');
    }

    function get_ulang($id) {
        if ( empty($id) )
            return false;
        return $this->get_row('/ulangs/ulang[id="'.$id.'"]');
    }

    function insert_ulang($data) {
        if ( trim( $data['id'] ) == "" ) {
            $this->last_error = new Owl_Error('507',__('Invalid language identity'));
            return false;
        }
        if ($this->get_ulang($data['id']) !== false ) {
            $this->last_error = new Owl_Error('507',__('Language identity is duplicate'));
            return false;
        }        
        $default_languages = array('id','en','vi','jp','fr');
        foreach (array_keys($data) as $lang) {           
            if (!in_array($lang, $default_languages)) {
                unset ($data[$lang]);
            }
        }
        $data = Owl_Format::esc_html_deep($data);
        return $this->insert('/ulangs', 'ulang', $data);
    }

    function update_ulang($id, $data) {
        if ( empty($id) )
            return false;

        $old_lang = $this->get_ulang($id);
        if ($old_lang === false) {
            $this->last_error = new Owl_Error('507','Lang identity is not exist');
            return false;
        }
        $data['id'] = $id;
        $data = Owl_System_Helper::array_merge($data, $old_lang);
        $data = Owl_Format::esc_html_deep($data);
        return $this->update('/ulangs/ulang[id="'.$id.'"]','ulang',$data);
    }

    function delete_ulang($id) {
        if ( empty($id) )
            return false;
        return $this->delete('/ulangs/ulang[id="'.$id.'"]');
    }
}